//
//  RegistrationView.m
//  ChunLaiZhuangClient
//
//  Created by zzs on 16/2/25.
//  Copyright © 2016年 CLZ. All rights reserved.
//

#import "RegistrationView.h"
#import "LoginViewController.h"
@implementation RegistrationView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)awakeFromNib{
    self.bgView1.layer.cornerRadius=5;
    self.bgView2.layer.cornerRadius=5;
    self.bgView3.layer.cornerRadius=5;
    self.bgView4.layer.cornerRadius=5;
    self.registerBt.layer.cornerRadius=5;
    [self.mobileTF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.validateCodeTF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [self.passwordTF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    self.validateCodeTF.delegate=self;
}
- (IBAction)getValidateCodeAction:(id)sender {
    if (![self.mobileTF.text isMobileNumber]) {
        [[LKDHud shareHud] showtip:@"请输入正确的手机号"];
        return;
    }
     [[LKDHud shareHud] showLoadingView];
    [ApiClient sendRegiseterSmsTelephone:self.mobileTF.text didFinishLoaded:^(KXJson *json, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        if ([[json getStringForKey:@"status"] intValue]==1) {
            [self timeFire];
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
}
- (IBAction)registrationAction:(id)sender {
    if (![self.mobileTF.text isMobileNumber]) {
        [[LKDHud shareHud] showtip:@"请输入正确的手机号"];
        return;
    }
    if (self.validateCodeTF.text.length==0) {
        [[LKDHud shareHud] showtip:@"验证码不能为空"];
        return;
    }
    if (self.passwordTF.text.length==0) {
        [[LKDHud shareHud] showtip:@"密码不能为空"];
        return;
    }
    if (!self.agreeBt.selected) {
        [[LKDHud shareHud] showtip:@"请选择用户协议"];
        return;
    }
     [[LKDHud shareHud] showLoadingView];
    [ApiClient registerNewUserTelephone:self.mobileTF.text sms:self.validateCodeTF.text password:self.passwordTF.text DidFinishLoaded:^(AccountModel *model, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
        if ([model.status intValue]==1) {
            LoginViewController *loginVC=(LoginViewController*)[RegistrationView viewController:self];
            [loginVC onLoginWithAccount:model];
            [loginVC dismissAction:nil];
            
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
         [[LKDHud shareHud] hideLoadingView];
    }];
    
  }

- (IBAction)agreeAction:(id)sender {
    UIButton *bt=(UIButton*)sender;
    bt.selected=!bt.selected;
}

- (IBAction)toAgreeProtocolAction:(id)sender {
    UIViewController *vc = [UIView viewController:self];
    CLZWebviewViewController *webVC=[[CLZWebviewViewController alloc] init];
    webVC.webTitle=@"常见问题";
    webVC.webUrl=[NSString stringWithFormat:@"%@%@",DOMAIN_ADDRESS,URL_USER_PROTOCOL];
    [vc.navigationController pushViewController:webVC animated:YES];
}

- (IBAction)isSecurityAction:(id)sender {
    UIButton *bt=(UIButton*)sender;
    bt.selected=!bt.selected;
    self.passwordTF.secureTextEntry=bt.selected;
}

- (void)timeFire
{
    if (!self.verifyCodeButton.isEnabled) {
        return;
    }
    
    _timeout=59; //倒计时时间
    [self.verifyCodeButton  setEnabled:NO];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
}

-(void)timeFireMethod{
    if (_timeout > 0) {
        _timeout--;
    }else{
        _timeout = 0;
    }
    
    if(_timeout==0){
        self.getCodeLabel.text=@"重新获取";
        [self.verifyCodeButton setTitle:@""  forState:UIControlStateNormal];
        [self.verifyCodeButton setEnabled:YES];
        [_countDownTimer invalidate];
    }else{
        int seconds = _timeout % 60;
        [self.verifyCodeButton setEnabled:NO];
        self.getCodeLabel.text=@"";
        NSString *strTime = [NSString stringWithFormat:@"%.2ds", seconds];
        self.verifyCodeButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.verifyCodeButton.titleLabel.text = strTime;
        [self.verifyCodeButton setTitle:strTime  forState:UIControlStateNormal];
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location >= 6)
        return NO; // return NO to not change text
    return YES;
}

- (void)dismissKeyboard:(UITapGestureRecognizer *)gesture {
    [self.mobileTF resignFirstResponder];
    [self.passwordTF resignFirstResponder];
    [self.validateCodeTF resignFirstResponder];
}
@end
